﻿using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdverseEventReportSystem.BLL
{
    public class PersonnelManager : BaseManager
    {
        /// <summary>
        /// 添加/编辑人员档案
        /// </summary>
        /// <param name="personnelModel"></param>
        /// <returns></returns>
        public static OperateResult Save(PersonnelModel personnelModel)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                Loginuser loginUser = LoginManager.GetLoginUser();
                P_Personnel personnel = personnelModel.P_Personnel;
                if (personnel.Id == 0)
                {
                    personnel.CreateTime = DateTime.Now;
                    personnel.CreateUserId = loginUser.Personnel_id;
                }
                else
                {
                    personnel.UpdateTime = DateTime.Now;
                    personnel.UpdateUserId = loginUser.Personnel_id;
                }
                if (personnel.Status == 1 || personnel.Status == 2)
                {
                    personnel.CommitTime = DateTime.Now;
                }
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfP_Personnel result = service.setPersonnelModel(personnelModel);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 生成档案编号
        /// </summary>
        /// <returns></returns>
        public static OperateResult GetPersonnelNo()
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfString result = service.getPersonnelNo();
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取人员档案详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static OperateResult GetPersonnelInfo(string id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                if (id == "0")
                {
                    id = "";
                }
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfPersonnelModel result = service.getPersonnelModel(id, LoginManager.GetLoginUser().Personnel_id);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static OperateResult DelFile(string path)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string fileDir = WebUtils.GetMapPath(path);
                IOHelper.DeleteFile(fileDir);
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <param name="reviewReason"></param>
        /// <returns></returns>
        public static OperateResult Review(int id, int status, string reviewReason)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                Loginuser loginUser = LoginManager.GetLoginUser();
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfPersonnelModel result = service.getPersonnelModel(id.ToString(), "");

                if (result.S)
                {
                    PersonnelModel personnelModel = result.D;
                    P_Personnel personnel = personnelModel.P_Personnel;
                    if (personnel.Status != 1 && personnel.Status != 2)
                    {
                        resultInfo.Result = 0;
                    }

                    if (resultInfo.Result == 1)
                    {
                        personnel.Status = status;
                        if (status == 2 || status == 3)
                        {
                            personnel.ReviewReason = reviewReason;
                            personnel.ReviewTime = DateTime.Now;
                            personnel.ReviewUserID = loginUser.Personnel_id;
                        }
                        else
                        {
                            personnel.ReviewReason1 = reviewReason;
                            personnel.ReviewTime1 = DateTime.Now;
                            personnel.ReviewUserID1 = loginUser.Personnel_id;
                        }
                        ResultHelperOfP_Personnel result1 = service.setPersonnelModel(personnelModel);
                        if (!result1.S)
                        {
                            resultInfo.Result = 0;
                            resultInfo.Body = result1.M;
                        }
                    }
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 添加培训进修
        /// </summary>
        /// <param name="trainingRecord"></param>
        /// <returns></returns>
        public static OperateResult AddTrainingRecord(P_TrainingRecord trainingRecord)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfInt32 result = service.Add_TrainingRecord(trainingRecord);

                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 添加论文发表
        /// </summary>
        /// <param name="paperRecord"></param>
        /// <returns></returns>
        public static OperateResult AddPaperRecord(P_PaperRecord paperRecord)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfInt32 result = service.Add_PaperRecord(paperRecord);

                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 添加科研情况
        /// </summary>
        /// <param name="researchRecord"></param>
        /// <returns></returns>
        public static OperateResult AddResearchRecord(P_ResearchRecord researchRecord)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfInt32 result = service.Add_ResearchRecord(researchRecord);

                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 添加奖惩记录
        /// </summary>
        /// <param name="rewardAndPunishRecord"></param>
        /// <returns></returns>
        public static OperateResult AddRewardAndPunishRecord(P_RewardAndPunishRecord rewardAndPunishRecord)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfInt32 result = service.Add_RewardAndPunishRecord(rewardAndPunishRecord);

                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
    }
}
